package com.ziven.common.widget.value;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.ziven.common.widget.R;
import com.ziven.easygo.util.LogHelper;
import com.ziven.easygo.util.ViewHelper;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author Ziven
 */
public final class Gravity extends Value {

    private static final String TAG = "GravityTag";
    private static final int PARENT_GRAVITY = 101;
    private static final int PARENT_GRAVITY_TAG = 102;

    public static Gravity center() {
        return size(android.view.Gravity.CENTER);
    }

    public static Gravity horizontal() {
        return size(android.view.Gravity.CENTER_HORIZONTAL);
    }

    public static Gravity vertical() {
        return size(android.view.Gravity.CENTER_VERTICAL);
    }

    public static Gravity start() {
        return size(android.view.Gravity.START);
    }

    public static Gravity end() {
        return size(android.view.Gravity.END);
    }

    public static Gravity top() {
        return size(android.view.Gravity.TOP);
    }

    public static Gravity bottom() {
        return size(android.view.Gravity.BOTTOM);
    }

    public static Gravity gravity(int gravity) {
        return size(gravity);
    }

    public static Gravity size(int gravity) {
        return new Gravity().accept(my -> my.value = gravity);
    }

    public Gravity parentGravity() {
        resId(PARENT_GRAVITY);
        return this;
    }

    public Gravity parentGravityTag() {
        resId(PARENT_GRAVITY_TAG);
        return this;
    }

    private Gravity() {}

    @Override
    protected void takeEffect(@NonNull ViewHelper<? extends View> helper) {
        value(Integer.class, value -> helper.getViewNulls().doNotNull(view -> {
            LogHelper.log(TAG, "resId=" + resId, "value=" + value);
            if(resId == PARENT_GRAVITY || resId == PARENT_GRAVITY_TAG) {
                try {
                    ViewGroup.LayoutParams params = view.getLayoutParams();
                    Field field = params.getClass().getDeclaredField("gravity");
                    if(!field.isAccessible()) {
                        field.setAccessible(true);
                    }
                    field.setInt(params, value);
                    view.setLayoutParams(params);
                } catch (NoSuchFieldException | IllegalAccessException | NullPointerException e) {
                    if(resId == PARENT_GRAVITY_TAG) {
                        view.setTag(R.id.easy_common_widget_id_gravity, value);
                    }
                    LogHelper.of(TAG).always().throwable(e).print();
                }
                return;
            }
            try {
                LogHelper.log(TAG, "value=" + value + " view=" + view);
                Method method = view.getClass().getMethod("setGravity", int.class);
                if (!method.isAccessible()) {
                    method.setAccessible(true);
                }
                method.invoke(view, value);
            } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                LogHelper.of(TAG).always().throwable(e).print();
            }
        }));
    }
}
